package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 广播某人下线,广播当前在线人数
 */
public class Server5 {
    private List<PrintWriter> allClientOut=new ArrayList<>();
    private ServerSocket serverSocket;

    public Server5() throws IOException {
        serverSocket = new ServerSocket(8088);
        System.out.println("启动服务器成功");
    }

    void start() {
        try {
            while (true) {
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();
                System.out.println(socket.getInetAddress().getHostAddress()+"连接上了");
                Thread th = new Thread(new ClientHandler(socket));
                th.start();
            }
            // System.out.println(is.read());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        Server5 server = new Server5();
        server.start();
    }

    class ClientHandler implements Runnable {
        private Socket socket;
        private String host;
        ClientHandler(Socket s) {
            this.socket = s;
            this.host=socket.getInetAddress().getHostAddress();
        }

        @Override
        public void run() {
            PrintWriter pw=null;
            try {
                InputStream is = null;
                is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                OutputStream out =socket.getOutputStream();
                OutputStreamWriter osw=new OutputStreamWriter(out, StandardCharsets.UTF_8);
                BufferedWriter bw=new BufferedWriter(osw);
                pw=new PrintWriter(bw,true);
                //并发安全处理
                synchronized (allClientOut){
                    allClientOut.add(pw);
                }
                sendMessage(host+"上线了,当前在线人数:"+allClientOut.size());
                String line;
                // System.out.println("客户端说:"+br.readLine());
                // while ((line=br.readLine())!=null)
                while (true) {
                    if (!socket.isConnected() || (line=br.readLine()) == null)
                        break;
                    System.out.println(host+"说:"+line);
                    //把某人的语录公告出去
                    sendMessage(host+"说:"+line);
                }
            } catch (IOException e) {
                //e.printStackTrace();
            }finally {
                //客户端断开
                synchronized (allClientOut){
                    allClientOut.remove(pw);
                }
                sendMessage(host+"下线了!"+"当前在线人数:"+allClientOut.size());
                socket.isClosed();
            }
        }
        //广播
        private void sendMessage(String message){
            System.out.println(message);
            synchronized (allClientOut){
                for (int i=0;i<allClientOut.size();i++)
                    allClientOut.get(i).println(message);
            }

        }
    }
}
